// https://leetcode.cn/problems/invert-binary-tree/submissions/498182459/

// 先将左右子节点交换位置，然后将其依次入栈，再出栈，这样是想将左边的依次翻转，再将右边的依次翻转

import TreeNode from "./TreeNode";


function invertTree(root: TreeNode | null): TreeNode | null {

    if(!root) return null;
    // 1.创建栈结构
    const stack = [root];

    // 2.从栈中不断取出节点，依次对左右节点进行交换

    while(stack.length){
        let current = stack.pop()!;

        // 对current节点进行左右交换
        const temp = current.left;
        current.left = current.right;;
        current.right = temp;

        // 将子节点加入栈中
        if(current.left){
            stack.push(current.left)
        }
        if(current.right){
            stack.push(current.right)
        }

    }


    
    return root;
};

export {}